package com.tianyuan.lims.erp.admin.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.alibaba.fastjson.JSON;
import com.jfinal.aop.Clear;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.paragetter.Para;
import com.jfinal.json.FastJson;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.tianyuan.lims.common.config.redis.Redis;
import com.tianyuan.lims.common.config.redis.RedisManager;
import com.tianyuan.lims.common.constant.BaseConstant;
import com.tianyuan.lims.dms.entity.LimsArea;
import com.tianyuan.lims.erp.admin.entity.AdminUser;
import com.tianyuan.lims.erp.admin.service.AdminRoleService;
import com.tianyuan.lims.erp.admin.service.AdminUserService;
import com.tianyuan.lims.utils.BaseUtil;
import com.tianyuan.lims.utils.CommonCryptogramUtil;
import com.tianyuan.lims.utils.R;
import com.tianyuan.lims.utils.ValidateCodeUtils;
import io.github.yedaxia.apidocs.ApiDoc;
import org.apache.commons.lang3.StringUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;

/**
 * 用户登录
 *
 * @author z
 */
@Clear
public class AdminLoginController extends Controller {
    @Inject
    private AdminUserService adminUserService;

    @Inject
    private AdminRoleService adminRoleService;

//    @Inject
//    private HwSmsSenderService hwSmsSenderService;

    public final static Prop prop = PropKit.use("config/tylims-config.cfg");

    public void index() {
         //redirect("/index.html");
        //renderJson("ok");
        //redirect("/app/index");
    }

    public void apidoc() {
        redirect("/japidoc/V1.0/index.html");
    }

    /**
     * @param username 用户名
     * @param password 密码
     * @author zhangzhiwei
     * 用户登录
     */
    public void loginV1(@Para("username") String username, @Para("password") String password) {

        String key = BaseConstant.USER_LOGIN_ERROR_KEY + username;
        Redis redis = RedisManager.getRedis();
        boolean enableRedis = redis == null ? false : true;
        if (enableRedis) {
            long beforeTime = System.currentTimeMillis() - 60 * 5 * 1000;
            if (redis.exists(key)) {
                if (redis.zcount(key, beforeTime, System.currentTimeMillis()) >= 15) {
                    Set zrevrange = redis.zrevrange(key, 4, 5);
                    Long time = (Long) zrevrange.iterator().next() + 60 * 5 * 1000;
                    long expire = (time - System.currentTimeMillis()) / 1000;
                    renderJson(R.error("密码错误次数过多，请等" + expire + "秒后在重试！"));
                    return;
                }
            }
            redis.zadd(key, System.currentTimeMillis(), System.currentTimeMillis());
        }
        if (StrUtil.isEmpty(username) || StrUtil.isEmpty(password)) {
            java.util.Map<String, Object> args = FastJson.getJson().parse(getRawData(), java.util.Map.class);
            if (args.get("username") != null) username = args.get("username").toString();
            if (args.get("username") != null) password = args.get("password").toString();
            if (StrUtil.isEmpty(username) || StrUtil.isEmpty(password)) {
                renderJson(R.error("请输入用户名和密码！"));
                return;
            }
        }
        AdminUser user = AdminUser.dao.findFirst(Db.getSql("admin.user.queryByUserName"), username.trim());
        if (user == null) {
            renderJson(R.error("用户名或密码错误！"));
            return;
        }
        if (user.getStatus() == 0) {
            renderJson(R.error("账户被禁用！"));
            return;
        }
        if (BaseUtil.verify(username + password, user.getSalt(), user.getPassword())) {
            if (user.getStatus() == 2) {
                user.setStatus(1);
            }

            String token = IdUtil.simpleUUID();
            user.setLastLoginIp(ServletUtil.getClientIP(getRequest()));
            user.setLastLoginTime(new Date());
            user.update();
            user.setRoles(adminRoleService.queryRoleIdsByUserId(user.getUserId()));
            Integer type = getParaToInt("type", 1);

            if (enableRedis) {
                redis.del(key);
                redis.setex(token, 3600, user);
                BaseUtil.setToken(user.getUserId(), token, type);
            }

            user.remove("password", "salt");
            setSessionAttr("user", user);
            setSessionAttr("token", token);

            renderJson(R.ok().put("Admin-Token", token).put("user", user).put("auth", adminRoleService.auth(user.getUserId())));
        } else {
            Log.getLog(getClass()).warn("用户登录失败");
            renderJson(R.error("用户名或密码错误！"));
        }

    }

    /**
     *  登录
     * @param d 2898或1263
     * @param u_identifier
     * @param credential_key
     */
//    public void login() {
//
////        String key = BaseConstant.USER_LOGIN_ERROR_KEY + username;
////        Redis redis = RedisManager.getRedis();
////        boolean enableRedis = redis == null ? false : true;
////        if (enableRedis) {
////            long beforeTime = System.currentTimeMillis() - 60 * 5 * 1000;
////            if (redis.exists(key)) {
////                if (redis.zcount(key, beforeTime, System.currentTimeMillis()) >= 15) {
////                    Set zrevrange = redis.zrevrange(key, 4, 5);
////                    Long time = (Long) zrevrange.iterator().next() + 60 * 5 * 1000;
////                    long expire = (time - System.currentTimeMillis()) / 1000;
////                    renderJson(R.error("密码错误次数过多，请等" + expire + "秒后在重试！"));
////                    return;
////                }
////            }
////            redis.zadd(key, System.currentTimeMillis(), System.currentTimeMillis());
////        }
//        String username = "";
//        String password = "" ;
//        String d = "" ;
//        Map<String, Object> args = FastJson.getJson().parse(getRawData(), Map.class);
//        if (args.get("u_identifier") != null) username = args.get("u_identifier").toString();
//        if (args.get("credential_key") != null) password = args.get("credential_key").toString();
//        if (args.get("d") != null) d = args.get("d").toString();
//        if (StrUtil.isEmpty(username) || StrUtil.isEmpty(password)) {
//            renderJson(R.error("请输入用户名和密码！"));
//            return;
//        }
//
//
//        d = CommonCryptogramUtil.doSm2Decrypt(d);
//        username = CommonCryptogramUtil.doSm2Decrypt(username);
//        password = CommonCryptogramUtil.doSm2Decrypt(password);
//
//
//
//        AdminUser user = AdminUser.dao.findFirst(Db.getSql("admin.user.queryByUserName"), username.trim());
//        if (user == null) {
//            renderJson(R.error("用户名或密码错误！"));
//            return;
//        }
//        if (user.getStatus() == 0) {
//            renderJson(R.error("账户被禁用！"));
//            return;
//        }
//        // d 回 陕西省 江西省 西安市那个字段orgtypeId    还有pid provinceid
//        Integer areaid = user.getAreaid();
//        Integer orgTypeId = user.getUsertype();
//
//        Integer deptId = user.getProvinceid();
//        if(!d.equals(String.valueOf(deptId))){
//            renderJson(R.error("用户名或密码错误~！"));
//            return;
//        }
//
//        String areaName = "";
//        String deptName = "";
//        Integer areaPid = 0;
//        String lng = "";
//        String lat = "";
//        float zoom = 0f;
//
//        if(areaid != null) {
//            LimsArea area = LimsArea.dao.findFirst("select * from lims_area where id = " + areaid);
//            areaName = area.getName();
//            areaPid = area.getPid();
//            lng = area.getLng();
//            lat = area.getLat();
//            zoom = Float.parseFloat(area.getZoom());
//        }
//
//        if(areaid != null) {
//            LimsArea area = LimsArea.dao.findFirst("select * from lims_area where id = " + deptId);
//            deptName = area.getName();
//        }
//
//
//        if (BaseUtil.verify(username + password, user.getSalt(), user.getPassword())) {
//            if (user.getStatus() == 2) {
//                user.setStatus(1);
//            }
//
//            String token = IdUtil.simpleUUID();
//            user.setLastLoginIp(ServletUtil.getClientIP(getRequest()));
//            user.setLastLoginTime(new Date());
//            user.update();
//            user.setRoles(adminRoleService.queryRoleIdsByUserId(user.getUserId()));
//            Integer type = getParaToInt("type", 1);
//
////            if (enableRedis) {
////                redis.del(key);
////                redis.setex(token, 3600, user);
////                BaseUtil.setToken(user.getUserId(), token, type);
////            }
//
//            user.put("areaName", areaName);
//            user.put("deptName", deptName);
//            user.put("areaPid", areaPid);
//            user.put("lng", lng);
//            user.put("lat", lat);
//            user.put("zoom", zoom);
//
//
//            user.remove("password", "salt");
//            setSessionAttr("user", user);
//            setSessionAttr("token", token);
//            BaseUtil.setUser(user);
//            renderJson(R.ok().put("Admin-Token", token).put("user", user).put("auth", adminRoleService.auth(user.getUserId())));
//        } else {
//            Log.getLog(getClass()).warn("用户登录失败");
//            renderJson(R.error("用户名或密码错误！"));
//        }
//
//    }



    public void resetPassword(@Para("userIds") String ids, @Para("password") String pwd) {
        if (StrUtil.isEmpty(ids) || StrUtil.isEmpty(pwd)) {
            if (StrUtil.isEmpty(getRawData())) {
                renderJson(R.error("参数！"));
                return;
            }

            Map<String, Object> args = FastJson.getJson().parse(getRawData(), Map.class);
            if (args.get("userIds") != null) ids = args.get("userIds").toString();
            if (args.get("pwd") != null) pwd = args.get("password").toString();
            if (StrUtil.isEmpty(ids) || StrUtil.isEmpty(pwd)) {
                renderJson(R.error("请输入用户名和密码！"));
                return;
            }
        }
        renderJson(adminUserService.resetPassword(ids, pwd));
    }

    /**
     * @author zhangzhiwei
     * 退出登录
     */
    public void logout() {
        String token = BaseUtil.getToken(getRequest());
        if (!StrUtil.isEmpty(token)) {
            RedisManager.getRedis().del(token);
            removeCookie("Admin-Token");
        }
        renderJson(R.ok());
    }

    public void version() {
        renderJson(R.ok().put("name", BaseConstant.NAME).put("version", BaseConstant.VERSION));
    }


    public void ping() {
        List<String> arrays = new ArrayList<>();
        Connection connection = null;
        try {
            connection = Db.use().getConfig().getConnection();
            if (connection != null) {
                arrays.add("数据库连接成功");
            }
        } catch (Exception e) {
            arrays.add("数据库连接异常");
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    Log.getLog(getClass()).error("", e);
                }
            }

        }
        try {
            String ping = RedisManager.getRedis().ping();
            if ("PONG".equals(ping)) {
                arrays.add("Redis配置成功");
            } else {
                arrays.add("Redis配置失败");
            }
        } catch (Exception e) {
            arrays.add("Redis配置失败");
        }
        renderJson(R.ok().put("data", arrays));
    }

    /**
     * 注册
     *
     * @JSonParam("username") String username  用户名,
     * @JsonParam("password") String password  密码,
     * @JsonParam("email") String email  邮箱,
     * @JsonParam("mobile") String mobile  手机号,
     * @JsonParam("realname") String realname  机构名称,
     * @JsonParam("dept_id") Integer dept_id  所属机构编码,
     * @JsonParam("areaid") Integer areaid  所属省份编码,
     */
    @ApiDoc(url = "/register", method = "post")
    public void register() {
        try {
            String data = getRawData();

            AdminUser adminUser = JSON.parseObject(data, AdminUser.class);
            String username = CommonCryptogramUtil.doSm2Decrypt(adminUser.getUsername());
            String password = CommonCryptogramUtil.doSm2Decrypt(adminUser.getPassword());
            AdminUser user = AdminUser.dao.findFirst(Db.getSql("admin.user.queryByUserName"), adminUser.getUsername().trim());
            if (user != null) {
                renderJson(R.error("用户名已存在！"));
                return;
            }
            boolean isSave;
            String salt = IdUtil.simpleUUID();
            adminUser.setSalt(salt);

            adminUser.setUsername(username);

            password = BaseUtil.sign(username + password, adminUser.getSalt());
            adminUser.setPassword(password);
            adminUser.setCreateTime(new Date());
            adminUser.setAreaid(adminUser.getAreaid());
            isSave = adminUser.save();
            renderJson(R.ok().put("isSave", isSave ? R.ok() : R.error()));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(R.error());
        }

    }



    // -----------------------------------------


    @ApiDoc(url = "/getVerifyCode", method = "post")
    public void getVerifyCode(@Para("phone") String phone) {
        try {
            if(StringUtils.isEmpty(phone)){
                renderJson(R.error());
            }
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            setSessionAttr(phone, code);
            //hwSmsSenderService.getVerifyCode(phone, code);
            renderJson(R.ok().put("msg", "验证码短信发送成功!"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(R.error());
        }
    }

    @ApiDoc(url = "/hnregister", method = "post")
    public void hnregister() {
        try {
            String sendPasswd = "";
            String data = getRawData();
            AdminUser adminUser = JSON.parseObject(data, AdminUser.class);
            String username = CommonCryptogramUtil.doSm2Decrypt(adminUser.getUsername());
            String password = CommonCryptogramUtil.doSm2Decrypt(adminUser.getPassword());
            sendPasswd = password;
            String phone = adminUser.getMobile();
            //String code = adminUser.getCode();
            //Object codeInSession = getSessionAttr(phone);
           // if (codeInSession != null && codeInSession.equals(code)) {
                AdminUser user = AdminUser.dao.findFirst(Db.getSql("admin.user.queryByUserName"), adminUser.getUsername().trim());
                if (user != null) {
                    renderJson(R.error("用户名已存在！"));
                    return;
                }
                boolean isSave;
                String salt = IdUtil.simpleUUID();
                adminUser.setSalt(salt);
                adminUser.setUsername(username);
                password = BaseUtil.sign(username + password, adminUser.getSalt());
                adminUser.setPassword(password);
                adminUser.setCreateTime(new Date());
                adminUser.setAreaid(adminUser.getAreaid());
                // hn 1827
//                hwSmsSenderService.sendPasswd2User(adminUser.getMobile(), adminUser.getMobile(), sendPasswd);
                isSave = adminUser.save();
                renderJson(R.ok().put("isSave", isSave ? R.ok() : R.error()));
            //}else{
               // renderJson(R.error("验证码错误！"));
           // }
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(R.error());
        }
    }




}
